package com.ts.enterprise.module.user.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ts.enterprise.common.service.CommonService;
import com.ts.enterprise.common.utils.RedisUtil;
import com.ts.enterprise.enums.EnableEnum;
import com.ts.enterprise.enums.RedisKeyEnum;
import com.ts.enterprise.module.user.entity.Dict;
import com.ts.enterprise.module.user.mapper.DictMapper;
import com.ts.enterprise.module.user.vo.DictResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dean
 * @since 2021-10-18
 */
@Service
public class DictService extends ServiceImpl<DictMapper, Dict> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private CommonService commonService;

    @Autowired
    private LoginService loginService;

    public List<Dict> listByType(Integer type) {
        LambdaQueryWrapper<Dict> eq = new LambdaQueryWrapper<Dict>()
                .orderByAsc(Dict::getSort).orderByAsc(Dict::getId);
        return super.list(eq);
    }


    public List<DictResponse> selectAll() {
        String json = RedisUtil.get(RedisKeyEnum.T_DICT_CACHE.getPrefix());
        if (false){
//            return JSON.parseArray(json, DictResponse.class);
        }
        LambdaQueryWrapper<Dict> eq = new LambdaQueryWrapper<Dict>();
        List<Dict> dictList = super.list(eq);
        List<DictResponse> treeChildren = createTreeChildren(dictList, 0);
        if (CollectionUtils.isNotEmpty(treeChildren)){
//            RedisUtil.set(RedisKeyEnum.T_DICT_CACHE.getPrefix(), JSONObject.toJSONString(treeChildren),RedisKeyEnum.T_DICT_CACHE.getExpiredTime());
        }
        return treeChildren;
    }

    protected List<DictResponse> createTreeChildren(List<Dict> list, Integer pid) {
        if (CollectionUtils.isNotEmpty(list)){
            list.sort(Comparator.comparing(Dict::getSort).thenComparing(Dict::getId));
        }
        List<DictResponse> childList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DictResponse tree = null;
            Dict treeChild = list.get(i);
            if (pid.equals(treeChild.getParentId())) {
                tree = new DictResponse();
                BeanUtils.copyProperties(treeChild, tree);
                tree.setChildren(createTreeChildren(list, treeChild.getId()));
            }
            if (tree != null) {
                childList.add(tree);
            }
        }
        return childList;
    }

}
